<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="5" :sm="24">
              <a-form-item label="开课学年">
                <a-select
                  show-search
                  allowClear
                  placeholder="请选择开课学年"
                  v-model="queryParam.schoolYearId"
                  @change="changeSchoolYear">
                  <a-select-option v-for="item of this.schoolYearList" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="开课学期">
                <a-select show-search allowClear placeholder="请选择开课学期" v-model="queryParam.termId">
                  <a-select-option v-for="item of this.schoolTermList" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="开课课程">
                <a-select show-search allowClear placeholder="请选择开课课程" v-model="queryParam.courseId">
                  <a-select-option v-for="item of this.courseList" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="实验老师">
                <a-select
                  show-search
                  allowClear
                  placeholder="请选择实验老师"
                  :disabled="!hasRole('sa') && !hasRole('ca')"
                  v-model="queryParam.teacherId">
                  <a-select-option v-for="item of this.teacherList" :key="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQueryParam">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table ref="table" :columns="columns" :data="loadData" :rowKey="(record) => record.id">
        <span slot="frequencyIndex" slot-scope="text, record"> 第{{ record.frequencyIndex }}轮 </span>
        <span slot="dayOfWeek" slot-scope="text, record">
          {{
            record.dayOfWeek
              .split(',')
              .sort()
              .map((item) => {
                return '周' + ['一', '二', '三', '四', '五'][item - 1]
              })
              .join('、')
          }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="goHomeworkForTeacher(record)">教学管理</a>
        </span>
      </s-table>
    </a-card>
  </div>
</template>
<script>
  import { STable, XDown } from '@/components'
  import { mapGetters } from 'vuex'
  import { hasRole } from '@/utils/permissions'
  import { courseTeachingPage } from '@/api/modular/main/courseTeachingManage'
  import { sysSchoolYearList } from '@/api/modular/system/sysSchoolYearManage'
  import { sysSchoolTermList } from '@/api/modular/system/sysSchoolTermManage'
  import { getUserByType } from '@/api/modular/system/userManage'
  import { courseList } from '@/api/modular/system/courseManage'

  export default {
    components: {
      STable,
      XDown
    },
    data() {
      return {
        // 查询参数
        queryParam: {
          schoolYearId: undefined,
          termId: undefined,
          courseId: undefined,
          teacherId: undefined
        },
        schoolYearList: [],
        schoolTermList: [],
        courseList: [],
        teacherList: [],
        // 表头
        columns: [
          {
            title: '开课课程',
            align: 'center',
            dataIndex: 'courseName'
          },
          {
            title: '实验项目',
            align: 'center',
            dataIndex: 'projectName'
          },
          {
            title: '轮次',
            align: 'center',
            dataIndex: 'frequencyIndex',
            scopedSlots: { customRender: 'frequencyIndex' }
          },
          {
            title: '开课时间',
            align: 'center',
            dataIndex: 'dayOfWeek',
            scopedSlots: { customRender: 'dayOfWeek' }
          },
          {
            title: '教室',
            align: 'center',
            dataIndex: 'classroomName'
          },
          {
            title: '学生数',
            align: 'center',
            dataIndex: 'numberOfStudent'
          },
          {
            title: '已完成作业',
            align: 'center',
            dataIndex: 'numberOfFinishedHomework'
          },
          {
            title: '已批改作业',
            align: 'center',
            dataIndex: 'numberOfCorrectedHomework'
          },
          {
            title: '未批改作业',
            align: 'center',
            dataIndex: 'numberOfUncorrectedHomework'
          },
          {
            title: '已打分',
            align: 'center',
            dataIndex: 'numberOfGraded'
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: (parameter) => {
          return courseTeachingPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        }
      }
    },
    computed: {
      ...mapGetters(['userInfo'])
    },
    created() {
      this.columns.push({
        title: '操作',
        width: '150px',
        align: 'center',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
      this.getSchoolYearList()
      this.getSchoolTermList()
      this.loadTeacherList()
      this.getCourseList()
    },
    methods: {
      resetQueryParam() {
        if (hasRole('sa') || hasRole('ca')) {
          this.queryParam = {
            schoolYearId: undefined,
            termId: undefined,
            courseId: undefined,
            teacherId: undefined
          }
        } else {
          this.queryParam = {
            ...this.queryParam,
            schoolYearId: undefined,
            termId: undefined,
            courseId: undefined
          }
        }
      },
      loadTeacherList(collegeId) {
        getUserByType({
          userType: 'tch',
          collId: collegeId,
          id: hasRole('sa') || hasRole('ca') ? null : this.userInfo.id
        }).then((res) => {
          if (res.success) {
            this.teacherList = res.data
            if (res.data.length === 1) this.queryParam.teacherId = this.userInfo.id
          }
        })
      },
      /**
       * load学年列表
       */
      getSchoolYearList() {
        sysSchoolYearList().then((res) => {
          if (res.success) {
            this.schoolYearList = res.data
          }
        })
      },
      /**
       * load开课学期
       */
      getSchoolTermList(schoolYearId) {
        sysSchoolTermList({
          schoolYearId: schoolYearId
        }).then((res) => {
          if (res.success) {
            this.schoolTermList = res.data
          }
        })
      },
      /**
       * load课程
       */
      getCourseList() {
        courseList({
          collId: this.queryParam.collegeId
        }).then((res) => {
          if (res.success) {
            this.courseList = res.data
          }
        })
      },
      handleOk() {
        this.$refs.table.refresh()
      },
      goHomeworkForTeacher(record) {
        this.$router.push({ name: 'HomeworkForTeacher', params: record })
      },
      changeSchoolYear(schoolYearId) {
        this.queryParam.termId = undefined
        this.getSchoolTermList(schoolYearId)
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }
</style>
